package com.hospital.register.core.controller.admin;

import com.hospital.register.common.result.Result;
import com.hospital.register.core.service.DepartmentService;
import com.hospital.register.model.core.Department;
import com.hospital.register.vo.core.admin.DepartmentTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "科室管理", description = "面向管理员")
@RestController
@RequestMapping("/admin/hosp/department")
@Validated
//@CrossOrigin
public class DepartmentAdminController {

    /**
     * 处理科室相关业务
     */
    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "查询医院所有科室列表，返回树形结构")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "hoscode", value = "医院的编号", dataTypeClass = String.class, paramType = "path",required = true)
    })
    @GetMapping("/getDeptList/{hoscode}")
    public Result<List<DepartmentTreeVo>> getDeptList(@PathVariable String hoscode) {
        //结果列表中的每一个成员是一个大科室
        //每一个大科室下面都有一个小科室列表
        //在数据库中，大科室并没有单独存储，只是在每一个小科室中存放了所在大科室的编号和名称

        List<Department> all = this.departmentService.findByHoscode(hoscode);
        //根据大科室分组
        Map<String, DepartmentTreeVo> groups = this.groupByBigcode(all);
        //在将所有大科室 合并成列表
        List<DepartmentTreeVo> tree = new ArrayList<>(groups.size());

        for (DepartmentTreeVo parentNode : groups.values()) {
            tree.add(parentNode);
        }

        return Result.ok(tree);
    }

    /**
     * 将科室列表按照大科室进行分组
     *
     * @param all 科室列表
     * @return 分好组的科室
     * key 是 大科室的编号，value 是小科室列表
     */
    private Map<String, DepartmentTreeVo> groupByBigcode(List<Department> all) {
        Map<String, DepartmentTreeVo> parents = new HashMap<>();
        for (Department one : all) {//便利每一个小科室
            //获取所在的大科室，没有就创建
            DepartmentTreeVo parent = parents.get(one.getBigcode());

            if (parent == null) {
                parent = new DepartmentTreeVo();
                parent.setDepcode(one.getBigcode());
                parent.setDepname(one.getBigname());
                parent.setChildren(new ArrayList<>(5));
                parents.put(one.getBigcode(), parent);
            }

            //将自身加入大科室
            DepartmentTreeVo child = new DepartmentTreeVo();
            child.setDepcode(one.getDepcode());
            child.setDepname(one.getDepname());

            parent.getChildren().add(child);
        }

        return parents;
    }

}
